package cn.siriusbot.siriuspro.bot;

import cn.siriusbot.siriuspro.admin.service.BotService;
import cn.siriusbot.siriuspro.admin.service.IntentService;
import cn.siriusbot.siriuspro.admin.service.ServerConfigService;
import cn.siriusbot.siriuspro.bot.api.*;
import cn.siriusbot.siriuspro.bot.api.impl.bot.ConfigApiImpl;
import cn.siriusbot.siriuspro.bot.api.impl.bot.ResourceApiImpl;
import cn.siriusbot.siriuspro.bot.api.impl.bot.SiriusUtilsImpl;
import cn.siriusbot.siriuspro.bot.application.SiriusApplicationInfo;
import cn.siriusbot.siriuspro.config.bean.BotPool;
import cn.siriusbot.siriuspro.config.bean.StatisticsPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class SiriusBotApi implements BotApi, BotApiBuilder {

    @Autowired
    AnnouncesApi announcesApi;

    @Autowired
    ApiPermissionApi apiPermissionApi;

    @Autowired
    AudioApi audioApi;
    @Autowired
    ChannelApi channelApi;

    @Autowired
    ChannelPermissionsApi channelPermissionsApi;

    @Autowired
    DMS_Api dmsApi;

    @Autowired
    ForumApi forumApi;

    @Autowired
    GuildApi guildApi;

    @Autowired
    MemberApi memberApi;

    @Autowired
    MessageApi messageApi;

    @Autowired
    MessageReactionApi messageReactionApi;

    @Autowired
    MessageSettingApi messageSettingApi;

    @Autowired
    NoSpeakApi noSpeakApi;

    @Autowired
    PinsMessageApi pinsMessageApi;

    @Autowired
    RoleApi roleApi;

    @Autowired
    ScheduleApi scheduleApi;

    @Autowired
    UserApi userApi;

    @Autowired
    BotPool botPool;
    @Autowired
    ServerConfigService serverConfigService;
    @Autowired
    StatisticsPool statisticsPool;
    @Autowired
    BotService botService;
    @Autowired
    IntentService intentService;
    @Autowired
    ConfigApiImpl configApi;
    @Autowired
    ResourceApiImpl resourceApi;

    @Autowired
    C2CApi c2CApi;

    @Autowired
    GroupApi groupApi;

    Map<String, BotApi> apiMap = new ConcurrentHashMap<>();

    @Override
    public BotApi build(SiriusApplicationInfo info) {
        ConfigApi config = configApi.build(info);
        ResourceApi resourceApi = this.resourceApi.build(info.getPackageName());
        SiriusBotApiExternal external = new SiriusBotApiExternal(
                info,
                this,
                botPool,
                serverConfigService,
                statisticsPool,
                botService,
                intentService,
                config,
                resourceApi);
        apiMap.put(info.getPackageName(), external);
        return external;
//        BotApi botApi = apiMap.get(info.getPackageName());
//        if (botApi == null) {
//            ConfigApi config = configApi.build(info.getPackageName());
//            SiriusBotApiExternal external = new SiriusBotApiExternal(info, this, botPool, serverConfigService, statisticsPool, botService, intentService, config);
//            apiMap.put(info.getPackageName(), external);
//            return external;
//        }
//        return botApi;
//        System.out.println();
    }


    /**
     * 资源文件API
     *
     * @return
     */
    @Override
    public ResourceApi resourceApi() {
        return null;
    }

    @Override
    public SiriusUtils siriusUtils() {
        return new SiriusUtilsImpl(this);
    }

    /**
     * 配置相关API
     *
     * @return
     */
    @Override
    public ConfigApi configApi() {
        return null;
    }

    @Override
    public BotManageApi botManageApi() {
        return null;
    }

    /**
     * @return
     */
    @Override
    public AnnouncesApi announcesApi() {
        return announcesApi;
    }

    /**
     * @return
     */
    @Override
    public ApiPermissionApi apiPermissionApi() {
        return apiPermissionApi;
    }

    /**
     * @return
     */
    @Override
    public AudioApi audioApi() {
        return audioApi;
    }

    /**
     * @return
     */
    @Override
    public ChannelApi channelApi() {
        return channelApi;
    }

    /**
     * @return
     */
    @Override
    public ChannelPermissionsApi channelPermissionsApi() {
        return channelPermissionsApi;
    }

    /**
     * @return
     */
    @Override
    public DMS_Api dmsApi() {
        return dmsApi;
    }

    /**
     * @return
     */
    @Override
    public ForumApi forumApi() {
        return forumApi;
    }

    /**
     * @return
     */
    @Override
    public GuildApi guildApi() {
        return guildApi;
    }

    /**
     * @return
     */
    @Override
    public MemberApi memberApi() {
        return memberApi;
    }

    /**
     * @return
     */
    @Override
    public MessageApi messageApi() {
        return messageApi;
    }

    /**
     * @return
     */
    @Override
    public MessageReactionApi messageReactionApi() {
        return messageReactionApi;
    }

    /**
     * @return
     */
    @Override
    public MessageSettingApi messageSettingApi() {
        return messageSettingApi;
    }

    /**
     * @return
     */
    @Override
    public NoSpeakApi noSpeakApi() {
        return noSpeakApi;
    }

    /**
     * @return
     */
    @Override
    public PinsMessageApi pinsMessageApi() {
        return pinsMessageApi;
    }

    /**
     * @return
     */
    @Override
    public RoleApi roleApi() {
        return roleApi;
    }

    /**
     * @return
     */
    @Override
    public ScheduleApi scheduleApi() {
        return scheduleApi;
    }

    /**
     * @return
     */
    @Override
    public UserApi userApi() {
        return userApi;
    }

    @Override
    public C2CApi c2CApi() {
        return c2CApi;
    }

    @Override
    public GroupApi groupApi() {
        return groupApi;
    }


}
